package com.hskn.hss.module.dlbjviolation.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdictitem.entity.CostDictItem;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.dlbjviolation.service.IDlbjViolationService;
import com.hskn.hss.module.dlbjviolation.vo.DlbjViolationVO;
import com.hskn.hss.module.ksection.mapper.KsectionMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@RestController
@RequestMapping("/hss/dlbjviolation")
public class DlbjViolationController {

    @Autowired
    private IDlbjViolationService dlbjViolationService;

    @Resource
    private DlbjViolationMapper dlbjViolationMapper;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = dlbjViolationService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "collectList", method = RequestMethod.POST)
    public AjaxResult<Object> collectList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = dlbjViolationService.collectList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "histogram", method = RequestMethod.POST)
    public AjaxResult<Object> histogram(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<Map<String, Object>> list = dlbjViolationService.histogram(params);
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "dlbj_violation", bean = DlbjViolation.class, beanMapper = DlbjViolationMapper.class, names = "depName, personName")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            dlbjViolationService.add(params);
            return AjaxResult.success("发布成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "dlbj_violation", idKey = "dlbjViolationId", bean = DlbjViolation.class, beanMapper = DlbjViolationMapper.class, names = "depName, personName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            dlbjViolationService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "dlbj_violation", idKey = "dlbjViolationIds", bean = DlbjViolation.class, beanMapper = DlbjViolationMapper.class, names = "depName, personName")
    @RequestMapping(value = "deal", method = RequestMethod.POST)
    public AjaxResult<Object> deal(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            dlbjViolationService.deal(params);
            return AjaxResult.success("处理成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @PostMapping("excelExport")
    public com.hskn.hss.core.domain.AjaxResult excelExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));

            QueryWrapper<DlbjViolation> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(DlbjViolation::getCreateTime);
            List<String> ids= Arrays.asList(params.getString("ids").split(","));
            if(StringUtils.isNotEmpty(ids) && !(ids.size() ==1 && StringUtils.isEmpty(String.valueOf(ids.get(0))))){
                queryWrapper.lambda().in(DlbjViolation::getDlbjViolationId,ids);
            }
            String personTypeId = params.getString("personTypeId");
            if (StringUtils.isNotEmpty(personTypeId)) {
                queryWrapper.lambda().eq(DlbjViolation::getPersonTypeId, personTypeId);
            }
            String dlbjViolationId = params.getString("dlbjViolationId");
            if (StringUtils.isNotEmpty(dlbjViolationId)) {
                queryWrapper.lambda().eq(DlbjViolation::getDlbjViolationId, dlbjViolationId);
            }
            String personTypeName = params.getString("personTypeName");
            if (StringUtils.isNotEmpty(personTypeName)) {
                queryWrapper.lambda().like(DlbjViolation::getPersonTypeName, personTypeName);
            }
            String personId = params.getString("personId");
            if (StringUtils.isNotEmpty(personId)) {
                queryWrapper.lambda().eq(DlbjViolation::getPersonId, personId);
            }
            String violationTypeId = params.getString("violationTypeId");
            if (StringUtils.isNotEmpty(violationTypeId)) {
                queryWrapper.lambda().eq(DlbjViolation::getViolationTypeId, violationTypeId);
            }
            String violationTypeName = params.getString("violationTypeName");
            if (StringUtils.isNotEmpty(violationTypeName)) {
                queryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, violationTypeName);
            }
            String startDate = params.getString("startDate");
            String endDate = params.getString("endDate");
            if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
                queryWrapper.lambda().between(DlbjViolation::getDate, startDate, endDate);
            }
            List<DlbjViolation> dlbjViolations = dlbjViolationMapper.selectList(queryWrapper);

            //工具类实例化
            ExcelUtil<DlbjViolationVO> util = new ExcelUtil<>(DlbjViolationVO.class);
            //数据载体转换
            List<DlbjViolationVO> dlbjViolationVO=JSONObject.parseArray(JSONObject.toJSONString(dlbjViolations),DlbjViolationVO.class);
            //导出
            return util.exportExcel(dlbjViolationVO,"人员违规");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }


    /**
     * 违规统计图
     * @param request
     * @return
     */
    @RequestMapping(value = "selectDlbjViolationForCharts", method = RequestMethod.POST)
    public AjaxResult<Object> selectDlbjViolationForCharts(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            DlbjViolation dlbjViolation=new DlbjViolation();
            if(params!=null){
                dlbjViolation=JSONObject.parseObject(params.toJSONString(),DlbjViolation.class);
            }
            return AjaxResult.success(dlbjViolationService.selectDlbjViolationForCharts(dlbjViolation));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
    /**
     * 违规统计表
     * @param request
     * @return
     */
    @RequestMapping(value = "selectDlbjViolationForTable", method = RequestMethod.POST)
    public AjaxResult<Object> selectDlbjViolationForTable(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            DlbjViolation dlbjViolation=new DlbjViolation();
            if(params!=null){
                dlbjViolation=JSONObject.parseObject(params.toJSONString(),DlbjViolation.class);
            }
            return AjaxResult.success(Tools.getDataTable(dlbjViolationService.selectDlbjViolationForTable(dlbjViolation)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
    /**
     * 违规明细表
     * @param request
     * @return
     */
    @RequestMapping(value = "selectDlbjViolationForTableDetail", method = RequestMethod.POST)
    public AjaxResult<Object> selectDlbjViolationForTableDetail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            DlbjViolation dlbjViolation=new DlbjViolation();
            if(params!=null){
                dlbjViolation=JSONObject.parseObject(params.toJSONString(),DlbjViolation.class);
            }
            return AjaxResult.success(dlbjViolationService.selectDlbjViolationForTableDetail(dlbjViolation));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

